package com.sfeir.poc.portal.gui.client.services;

import com.google.gwt.user.client.Window;
import com.sfeir.poc.portal.gui.client.EventAwareClass;
import com.sfeir.poc.portal.gui.client.events.ApplicationEvent;
import com.sfeir.poc.portal.gui.client.events.ApplicationEventType;
import com.sfeir.poc.portal.gui.client.services.feed.Feed;
import com.sfeir.poc.portal.gui.client.services.feed.FeedListener;

/**
 * This class is used to get a complete spreadsheet feed from a spreadsheet key
 * This allow to get sheets title
 * @author bostoen.v
 *
 */
public class SpreadsheetService extends EventAwareClass implements FeedListener {

	public void getSheetTitle(String spreadsheetKey) {
		final Feed feed = new Feed();
		feed.getFeed("http://spreadsheets.google.com/feeds/worksheets/" + 
				spreadsheetKey + "/public/values?alt=atom" + "#" + 
				System.currentTimeMillis(), this);
	}

	public void onActionFired(ApplicationEvent event) {
		super.onActionFired(event);
		if (event.getType().equals(ApplicationEventType.GET_SHEET_TITLE)) {
			getSheetTitle((String) event.getContext());
		}
	}

	public void onFailure() {
		Window.alert("Failure");	
	}

	public void onSuccess(Feed feed) {
		fireActionEvent(ApplicationEventType.UPDATE_SHEET_TITLE, feed);
	}
}
